<?php
class Home extends FrontEnd_Controller {

    function Home()
    {
        parent::FrontEnd_Controller();
        $this->load->library('validation');
        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('text');
        $this->load->helper('avis_helper');
        $this->load->model('Home_m');
        $this->load->library('session');
        $this->load->model('Home_content_m');
        $this->_data['err']='';	
        $this->_data['news_data'] = $this->Home_m->list_news();
        $this->_data['comment_news'] = $this->Home_m->list_comment();
        $this->_data['comment_content'] = $this->Home_content_m->list_comment();
    }

    function index()
    {
        $this->_load_view('home/index');
        
    }
    function introduce(){
        $this->_load_view('home/introduce');
    }
    function loginProcess(){

        $config = array(
        array('field' => 'user',
        'label' => '',
        'rules' => 'required' 
        ),
        array('field' => 'pass',
        'label' => '',
        'rules' => 'required' 
        )
        );
        $this->form_validation->set_rules($config);
        if($this->input->post('login')){
            $username = $this->input->post('user');
            $password = md5($this->input->post('pass'));
            if($this->form_validation->run() == TRUE){
                $query = $this->Home_m->login($username, $password);
                if($query->num_rows() > 0){
                    $row = $query->row();
                    $this->session->set_userdata('userid', $row->user_id);
                    $this->session->set_userdata('username', $row->username);
                    redirect(base_url());
                }else{
                    $this->_data['err']="Invalid Username or Password";
                    //echo 'here';die;    
                    $this->_load_view('home/index');       
                    //redirect(base_url());
                }       

                //$this->_load_view('home/index');

            }else{
                $this->_load_view('home/index');
            }
        }
    }

    function logout(){
        $this->session->sess_destroy();
        redirect(base_url());
    }

    function check_username(){
        $query = $this->Home_m->get_username($this->input->post('username'));
        if($query->num_rows()>0){
            $this->form_validation->set_message('check_username', '%s was exist');
            return FALSE;
        }else{
            return TRUE;
        }
    }

    function check_email(){
        $query = $this->Home_m->get_email($this->input->post('email'));
        if($query->num_rows()>0){
            $this->form_validation->set_message('check_email', '%s was exist');
            return FALSE;
        }else{
            return TRUE;
        }
    }
    
    function check_old_password(){
        $query = $this->Home_m->check_old_password($this->session->userdata('userid'), md5($this->input->post('oldpassword')));
        if($query->num_rows()>0){
            return TRUE;
        }else{
            $this->form_validation->set_message('check_old_password', '%s was wrong');
            return FALSE;
        }
    }

    function register(){
        $conregis = array(
        array('field' => 'firstname',
        'label' => 'First name',
        'rules' => 'trim|required|xss_clean' 
        ),
        array('field' => 'lastname',
        'label' => 'Last name',
        'rules' => 'trim|required|xss_clean' 
        ),
        array('field' => 'email',
        'label' => 'Email',
        'rules' => 'trim|required|valid_email|callback_check_email|xss_clean' 
        ),
        array('field' => 'username',
        'label' => 'User name',
        'rules' => 'trim|required|callback_check_username|xss_clean' 
        ),
        array('field' => 'password',
        'label' => 'Password',
        'rules' => 'trim|required|matches[confirmpassword]|md5|xss_clean' 
        ),
        array('field' => 'confirmpassword',
        'label' => 'Confirm password',
        'rules' => 'trim|required|xss_clean' 
        )
        );
        $this->form_validation->set_rules($conregis);
        if($this->input->post('register')){

            if($this->form_validation->run() == TRUE){

                $data = array(
                'username'=>$this->input->post('username'),
                'password'=>$this->input->post('password'),
                'first_name'=>$this->input->post('firstname'),
                'last_name'=>$this->input->post('lastname'),
                'email'=>$this->input->post('email')
                );
                $query = $this->Home_m->register($data);
                $query1 = $this->Home_m->get_user_id_max();
                if($query1->num_rows()>0){
                    $row = $query1->row();
                    $maxid = $row->max_id;
                    $this->session->set_userdata('userid', $maxid);
                    $this->session->set_userdata('username',$this->input->post('username'));

                } 
                $this->_load_view('home/registersuccess');

            }else{
                $this->_load_view('home/register');   
            }
        }else{
            $this->_load_view('home/register');
        }
    }
    function loadRegister(){
        $this->_load_view('home/register');
    }

    
    function loadPostContent(){
        $this->_load_view('home/postContent');
    }

    function uploadImage(){
        $config['upload_path']='./assets/news_img/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('home/postContent', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $firstname = $this->input->post('firstnamec');
            $lastname = $this->input->post('lastnamec');
            $description = $this->input->post('txtdes');
            $this->load->view('upload_success', $data);
        }
    }

    function contact(){
        $configcontact = array(
        array('field' => 'ctfirstname',
        'label' => 'First name',
        'rules' => 'required|xss_clean' 
        ),
        array('field' => 'ctlastname',
        'label' => 'Last name',
        'rules' => 'required|xss_clean' 
        ),
        array('field' => 'ctemail',
        'label' => 'Email',
        'rules' => 'required|valid_email|xss_clean' 
        ),
        array('field' => 'ctmessage',
        'label' => 'Message',
        'rules' => 'required|xss_clean' 
        )

        );
        $this->form_validation->set_rules($configcontact);

        if($this->input->post('sendmessage')){
            if($this->form_validation->run() === TRUE){
                $data = array(
                'id'          => '',
                'firstname'   => $this->input->post('ctfirstname'),
                'lastname'    => $this->input->post('ctlastname'),
                'email'       => $this->input->post('ctemail'),
                'message'     => $this->input->post('ctmessage')

                );
                $this->Home_m->insert_contact_infor($data);
                $this->_load_view("home/contact/contactsuccess"); 
            } else{
                $this->_load_view("home/contact/contact"); 
            }

        } else{
            $this->_load_view("home/contact/contact");
        }
    }

   
    
    function change_user_profile(){
        $conupdate = array(
        array('field' => 'firstnamec',
        'label' => 'First name',
        'rules' => 'trim|required|xss_clean' 
        ),
        array('field' => 'lastnamec',
        'label' => 'Last name',
        'rules' => 'trim|required|xss_clean' 
        ),
        array('field' => 'email',
        'label' => 'Email',
        'rules' => 'trim|required|valid_email|callback_check_email|xss_clean' 
        )
       
        );
        $this->form_validation->set_rules($conupdate);
        if($this->input->post('change')){

            if($this->form_validation->run() == TRUE){

                $data = array(
                'first_name'=>$this->input->post('firstnamec'),
                'last_name'=>$this->input->post('lastnamec'),
                'email'=>$this->input->post('email'),
                );
                $id = $this->session->userdata('userid');
                
                
                $query = $this->Home_m->change_user_profile($data, $id);
                $this->_load_view('home/change_profile_success');

            }else{
                $this->_data['query'] = $this->Home_m->get_user_profie($this->session->userdata('userid'));
                $this->_load_view('home/change_profile');  
            }
        }else{
                $this->_data['query'] = $this->Home_m->get_user_profie($this->session->userdata('userid'));
                $this->_load_view('home/change_profile');
        }
    }
    
    function change_user_password(){
        $conchangepass = array(
        array('field' => 'oldpassword',
        'label' => 'Old password',
        'rules' => 'trim|required|md5|xss_clean|callback_check_old_password'
        ),
        array('field' => 'newpassword',
        'label' => 'New password',
        'rules' => 'trim|required|matches[confirmpassword]|md5|xss_clean'
        ),
        array('field' => 'confirmpassword',
        'label' => 'Confirm password',
        'rules' => 'trim|required|xss_clean' 
        )
               
        );
        $this->form_validation->set_rules($conchangepass);
        if($this->input->post('changePass')){

            if($this->form_validation->run() == TRUE){

                $data = array(
                'password'=>$this->input->post('newpassword')
                );
                $id = $this->session->userdata('userid');
                
                
                $query = $this->Home_m->change_user_profile($data, $id);
                $this->_load_view('home/change_password_success');

            }else{
                $this->_data['query'] = $this->Home_m->get_user_profie($this->session->userdata('userid'));
                $this->_load_view('home/change_password');  
            }
        }else{
                $this->_data['query'] = $this->Home_m->get_user_profie($this->session->userdata('userid'));
                $this->_load_view('home/change_password');
        }
    }

    function list_contents(){
        $per_page = $this->uri->segment(3);
        if($per_page==''){
            $per_page=3;
        }
        $off_set = $this->uri->segment(4);
        if($off_set==''){
            $off_set=0;
        }
        $config['base_url'] = base_url().'index.php/home/list_contents/'.$per_page;
        $config['uri_segment'] = 4;
        $config['per_page'] = '3';
        $config['total_rows'] = $this->Home_content_m->count_paid_contents();
        if($config['total_rows']==$off_set&&$off_set>0){
            $off_set=$off_set-$per_page;
        }                                      
        $this->_data['total_row'] = $config['total_rows']; 
        $this->pagination->initialize($config);
        $this->_data['query'] = $this->Home_content_m->get_all_contents($per_page,$off_set);
        $this->_data['pagination']=$this->pagination->create_links();
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_load_view('home/contenu/list_contents');
    }

    function add_content(){
        $config = array(
        array(
        'field'   => 'firstnameu',
        'label'   => 'First name',
        'rules'   => 'required|xss_clean'
        ),
        array(
        'field'   => 'lastname',
        'label'   => 'Last name',
        'rules'   => 'required|xss_clean'
        ),
        array(
        'field'   => 'address',
        'label'   => 'Address',
        'rules'   => 'required|xss_clean'
        ),
        array(
        'field'   => 'txtDes',
        'label'   => 'Description',
        'rules'   => 'required|xss_clean'
        )
        );
        $this->form_validation->set_rules($config);
        if($this->input->post('submit')){
            if($this->form_validation->run()){

                $image_path = './assets/news_img';
                $thumb_path = './assets/thumbs';
                $config = array(
                'allowed_types' => "jpg|jpeg|gif|png",
                'upload_path' => $image_path,
                'max_size'=>2000
                );
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload())
                {
                    $this->_data['error'] = $this->upload->display_errors();
                    $this->_load_view('home/contenu/add_content');
                }    
                else
                {
                    $upload_info = $this->upload->data();
                    $config = array(
                    'source_image' => $upload_info['full_path'], //get original image
                    'new_image' => $thumb_path, //save as new image //need to create thumbs first
                    'maintain_ratio' => true,
                    'width' => 300,
                    'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $birthday=$this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_date');
                    $leave_day = $this->input->post('leave_year').'-'.$this->input->post('leave_month').'-'.$this->input->post('leave_day');

                    $data = array(
                    'name'=>$this->input->post('firstnameu'),
                    'prename'=>$this->input->post('lastname'),
                    'address'=>$this->input->post('address'),
                    'birthday'=>$birthday,
                    'death'=>$leave_day,
                    'date_post'=>mdate("%Y-%m-%d",time()),
                    'description'=>$this->input->post('txtDes'),
                    'image'=>$upload_info['file_name'],
                    'user_id'=>$this->session->userdata('userid') ,
                    'is_paid'=>'0'
                    );
                    $this->Home_content_m->post_content($data);
                    redirect(site_url('home/list_contents'));
                }

            } else{
                $this->_load_view('home/contenu/add_content');
            }
        } else{
            $this->_load_view('home/contenu/add_content');
        }
    }

    function content_detail(){
        $id = $this->uri->segment(3);
        $this->_data['query'] = $this->Home_content_m->get_content_by_id($id);
        $this->_load_view('home/contenu/content_detail');            
    }
    
    
    
    function list_news(){
        $this->_data['query'] = $this->Home_m->list_news();
        $this->_load_view('home/news/list_news');
    }
    
    function news_detail(){
        $id = $this->uri->segment(3);
        $this->_data['query'] = $this->Home_m->get_news_by_id($id);
        $this->_load_view('home/news/news_detail');            
    }
    
    function add_comment_news($newsid){
        $config = array(
        array(
        'field'   => 'comment',
        'label'   => 'Comment content',
        'rules'   => 'required|xss_clean'
        )
        );
        $this->form_validation->set_rules($config);
        if($this->input->post('postComment')){

            if($this->form_validation->run() == TRUE){
                $commentDelay = $this->session->set_userdata('commentDelay', 'waitAccept');
                $data = array(
                'comment'=>$this->input->post('comment'),
                'user_name'=>$this->session->userdata('username'),
                'date_comment'=>mdate("%Y-%m-%d %h:%i:%a",time()),
                'news_id'=>$newsid,
                'is_accepted'=>'0'
                
                );
                
                $query = $this->Home_m->add_comment($data);
                redirect(site_url('home/news_detail/'.'/'.$newsid));

            }else{
                $commentDelay = $this->session->set_userdata('commentDelay', 'wrongAccept');
                redirect(site_url('home/news_detail/'.'/'.$newsid));
            }
        }else{
               
                $this->_load_view('home/news/comment');
        }
    }
    
    function add_comment_content($contentid){
        $config = array(
        array(
        'field'   => 'comment',
        'label'   => 'Comment content',
        'rules'   => 'required|xss_clean'
        )
        );
        $this->form_validation->set_rules($config);
        if($this->input->post('postComment')){

            if($this->form_validation->run() == TRUE){
                $data = array(
                'comment'=>$this->input->post('comment'),
                'user_name'=>$this->session->userdata('username'),
                'date_comment'=>mdate("%Y-%m-%d %h:%i:%a",time()),
                'content_id'=>$contentid,
                'is_accepted'=>'0'
                
                );
                
                $query = $this->Home_content_m->add_comment($data);
                redirect(site_url('home/content_detail/'.'/'.$contentid));

            }else{
               
                redirect(site_url('home/content_detail/'.'/'.$contentid));
            }
        }else{
               
                //$this->_load_view('home/news/comment');
        }
    }
    
    
    function de_recherche(){                       
        $firstname = $this->input->post('nom');
        $name = $this->input->post('prenom');
        $year_die = $this->input->post('year_to_search');
        if($firstname==''){
            $firstname='_';
        }
        if($name==''){
            $name ='_';
        }
        if($year_die==''){
            $year_die='_';
        }
        $per_page = 4;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 7;            
        $config['total_rows'] = $this->Home_content_m->count_content_search($firstname,$name,$year_die);
        $this->_data['search_result'] = $this->Home_content_m->search_contents($firstname,$name,$year_die,$per_page,0);
        $config['base_url'] = site_url('home/pagination_search').'/'.$firstname.'/'.$name.'/'.$year_die.'/'.$per_page.'/'; 
        $this->pagination->initialize($config);  
        $this->_data['pagination'] = $this->pagination->create_links();      

        $this->_load_view('home/contenu/search_content');
    }
    function pagination_search($firstname,$name,$year_die,$per_page){
        $off_set = $this->uri->segment(7);      
        if ($off_set == FALSE) $offset=0;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 7;            
        $config['total_rows'] = $this->Home_content_m->count_content_search($firstname,$name,$year_die);
        $this->_data['search_result'] = $this->Home_content_m->search_contents($firstname,$name,$year_die,$per_page,$off_set);
        $config['base_url'] = site_url('home/pagination_search').'/'.$firstname.'/'.$name.'/'.$year_die.'/'.$per_page.'/'; 
        $this->pagination->initialize($config);  
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_load_view('home/contenu/search_content');
    }
    function site_info($setting_name){
        $this->_data['setting'] = $this->Home_m->get_setting_info($setting_name);
        $this->_load_view('home/site_info');
    }
    function forget_password(){
        $this->_load_view('home/forget_password');
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */